//
//  ZhuanViewController.m
//  MusicPlayer
//
//  Created by 李欢 on 15/5/30.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//
#import "RecommendLatestModel.h"
#import "SingerSongTableViewCell.h"
#import "SANetWorkingTask.h"
#import "ZhuanViewController.h"
#import "PlayMusicViewController.h"

@interface ZhuanViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, retain) UITableView * tableView;
@property (nonatomic, retain) NSMutableArray * array;
@property (nonatomic, copy) NSString * web;
@property (nonatomic, retain) NSMutableArray *urlArray;
@property (nonatomic, retain) NSMutableArray *urlArr;

@property (nonatomic, retain) UIImageView *myImageView;
@end

@implementation ZhuanViewController
- (void)dealloc
{
    [_urlArr release];
    [_urlArray release];
    [_array release];
    [_detailID release];
    [_tableView release];
    [super dealloc];
}


-(void)viewWillAppear:(BOOL)animated
{
    
    [super viewWillAppear:YES];
    [self.myImageView removeFromSuperview];
    
    //    self.navigationItem.title = @"音乐库";
    //    self.navigationController.navigationBar.translucent = NO;
    
    UIBarButtonItem *exits = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"back.png"] style:UIBarButtonItemStylePlain target:self action:@selector(exitsAction:)];
    self.navigationItem.leftBarButtonItem = exits;
    
    [self.view setBackgroundColor:[UIColor whiteColor]];
    //
    self.myImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    //
    
    NSUserDefaults *aa = [NSUserDefaults standardUserDefaults];
    
    
    if ([aa objectForKey:@"pic1"]) {
        
        //    if (aa) {
        UIImage *image = [UIImage imageNamed:[aa objectForKey:@"pic1"]];
        
        [_myImageView setImage:image];
    } else {
        [_myImageView setImage:[UIImage imageNamed:@"PIC2.png"]];
        
    }
    
    [self.view addSubview:_myImageView];
    [self.view bringSubviewToFront:self.tableView];
   
    
}

- (void)exitsAction:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.urlArray = [NSMutableArray array];
        self.array = [NSMutableArray array];
        self.urlArr = [NSMutableArray array];
        
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor clearColor];
    
    self.web = [NSString stringWithFormat:@"http://api.dongting.com/song/album/%@?app=ttpod&v=v7.9.1.2015050518&uid=&mid=iPhone5S&f=f320&s=s310&imsi=&hid=&splus=8.3&active=1&net=2&openudid=a32e3f40acd47460a764124ccb6894dcb5e20200&idfa=E2FD8DB1-FBA8-40EA-8C02-FAB446839AD1&utdid=VV",self.detailID];
    [SANetWorkingTask request:self.web parmater:nil block:^(NSDictionary *result) {
       
        NSMutableDictionary * dic  = [result objectForKey:@"data"];
        self.array = [dic objectForKey:@"songList"];
        for (NSDictionary *dic1 in self.array) {
            NSArray *arr = [dic1 objectForKey:@"urlList"];
            if (arr.count >= 2) {
                NSDictionary *d = [arr objectAtIndex:1];
                NSString *str = [d objectForKey:@"url"];
                [self.urlArr addObject:str];
            }
            RecommendLatestModel *model = [[RecommendLatestModel alloc]init];
            [model setValuesForKeysWithDictionary:dic];
            model.song_name = model.name;
            model.singer_name = model.singerName;
            [self.urlArray addObject:model];
        }

        [self.tableView reloadData];
    }];
    [self creatTableView];

}

//创建tableView
-(void)creatTableView
{

    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - self.view.frame.size.height/667.0 * 70 -self.view.frame.size.height/667.0 * 64) style:UITableViewStylePlain];

    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
    self.tableView.backgroundColor = [UIColor clearColor];

    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    self.tableView.rowHeight = self.view.frame.size.width/375.0* 80;
    [self.view addSubview:self.tableView];
    [self.tableView release];

}

//协议
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _array.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
        static NSString * cellIdentify = @"cell";
    
        SingerSongTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellIdentify];
        if (!cell) {
            cell = [[[SingerSongTableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentify] autorelease];
        }
    
    cell.backgroundColor = [UIColor clearColor];
    NSMutableDictionary * dic = [self.array objectAtIndex:indexPath.row];
    cell.name.text = [dic objectForKey:@"name"];
    cell.name.textColor = [UIColor whiteColor];
    
    cell.singerName.text = [dic objectForKey:@"singerName"];
    cell.singerName.textColor = [UIColor whiteColor];
    
        return cell;
    
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    RecommendLatestModel *model = [self.urlArray objectAtIndex:indexPath.row];
    NSString *str = [self.urlArr objectAtIndex:indexPath.row];
    PlayMusicViewController *playMusic = [[PlayMusicViewController alloc] init];
    model.song_name = model.name;
   model.singer_name = model.singerName;
    playMusic.songName = model.song_name;
    playMusic.singer = model.singer_name;
   playMusic.songUrl = str;
    playMusic.songArray = self.urlArr;
        playMusic.songListArray = self.urlArray;
    playMusic.num = indexPath.row;
    
    [self.navigationController pushViewController:playMusic animated:YES];
    [playMusic release];

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
  
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
